package com.ffcs.web.controllers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.dto.ShouChangResponse;
import com.ffcs.web.model.entity.Advert;
import com.ffcs.web.model.entity.CollectNews;
import com.ffcs.web.model.entity.PicNews;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.services.AdvertService;
import com.ffcs.web.services.CollectNewsService;
import com.ffcs.web.services.CommonService;
import com.ffcs.web.services.PicNewsService;
import com.ffcs.web.utils.CommonConfig;

@Controller
public class PicNewsController {

	private Logger logger = Logger.getLogger(PicNewsController.class);
	
	@Autowired
	private PicNewsService picNewsService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private AdvertService advertService;
	@Autowired
	private CollectNewsService collectNewsService;
	
	/**
	 * 进入轮播新闻页
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("gotoPicNews")
	public void gotoPicNews(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		String advertId=request.getParameter("advertId");
		Advert advert=advertService.getAdvert(advertId);
		if(advert.getType()==1){
		if(advert.getAdvertType()==1){
			PicNews picNews=picNewsService.getPicNews(advert.getAssociatedId()+"");
			WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
			boolean b=collectNewsService.isShouchang(picNews.getId(),wxUser.getId(),(short)1);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			picNews.setAddTimeStr(sdf.format(picNews.getAddTime()));
			request.setAttribute("picNews",picNews);
			request.setAttribute("advertId",advertId);
			if(b){
				request.setAttribute("isShouChang","1");
			}else{
				request.setAttribute("isShouChang","0");
			}
			request.getRequestDispatcher("newsInfo.jsp").forward(request, response);
			return ;
		}else{
			response.sendRedirect("gotoPeixunDesc?studyId="+advert.getAssociatedId());
		}
		}else{
			PicNews picNews=picNewsService.getPicNews(advert.getAssociatedId()+"");
			WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
			boolean b=collectNewsService.isShouchang(picNews.getId(),wxUser.getId(),(short)1);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			picNews.setAddTimeStr(sdf.format(picNews.getAddTime()));
			request.setAttribute("picNews",picNews);
			request.setAttribute("advertId",advertId);
			if(b){
				request.setAttribute("isShouChang","1");
			}else{
				request.setAttribute("isShouChang","0");
			}
			request.getRequestDispatcher("newsInfo.jsp").forward(request, response);
			return ;
		}
	}
	
	/**
	 * 进入轮播新闻页
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("gotoPicNews2")
	public void gotoPicNews2(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		String id=request.getParameter("id");
		PicNews picNews=picNewsService.getPicNews(id+"");
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		boolean b=collectNewsService.isShouchang(picNews.getId(),wxUser.getId(),(short)1);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		picNews.setAddTimeStr(sdf.format(picNews.getAddTime()));
		request.setAttribute("picNews",picNews);
		request.setAttribute("advertId",id);
		if(b){
			request.setAttribute("isShouChang","1");
		}else{
			request.setAttribute("isShouChang","0");
		}
		request.getRequestDispatcher("newsInfo.jsp").forward(request, response);
		return ;
	}
	
	@RequestMapping("isShouchang2")
	@ResponseBody
	public BaseResponse isShouchang2(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		String tel=request.getParameter("tel");
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		if(collectNewsService.isShouchang(tel, wxUser.getId())){
			baseResponse.setResultCode("1");
			baseResponse.setResultDesc("已收藏");
			return baseResponse;
		}else{
			baseResponse.setResultCode("2");
			baseResponse.setResultDesc("未收藏");
			return baseResponse;
		}
	}
	
	@RequestMapping("goShouchang2")
	@ResponseBody
	public BaseResponse goShouchang2(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		String tel=request.getParameter("tel");
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		try {
			collectNewsService.updateShouchang(tel, wxUser.getId());
		} catch (Exception e) {
			logger.error("用户:"+wxUser.getNickname()+"收藏咨询师:"+tel+"-成功");
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("failure");
			return baseResponse;
		}
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	/**
	 * 收藏
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("goShouchang")
	@ResponseBody
	public BaseResponse goShouchang(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		String picNewsId=request.getParameter("picNewsId");
		String type=request.getParameter("type");
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		try {
			collectNewsService.updateShouchang(picNewsId, wxUser.getId(),Short.parseShort(type));
		} catch (Exception e) {
			logger.error("用户:"+wxUser.getNickname()+"收藏文章:"+picNewsId+"-成功");
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("failure");
			return baseResponse;
		}
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
	
	/**
	 * 获取我的收藏
	 * @param request
	 * @return
	 */
	@RequestMapping("getAPicNews")
	@ResponseBody
	public BaseResponse getAPicNews(String id){
		BaseResponse baseResponse=new BaseResponse();
		PicNews picNews=picNewsService.getPicNews(id+"");
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc(picNews.getTitle());
		return baseResponse;
	}
	
	/**
	 * 获取我的收藏
	 * @param request
	 * @return
	 */
	@RequestMapping("getMyShouChangList")
	@ResponseBody
	public ShouChangResponse getMyShouChangList(HttpServletRequest request){
		ShouChangResponse shouChangResponse=new ShouChangResponse();
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		List<CollectNews> listTemp=collectNewsService.getMyShouChangList(wxUser);
		List<CollectNews> list=new ArrayList<CollectNews>();
		for (CollectNews collectNews : listTemp) {
			if(collectNews.getStatus()!=0){
				list.add(collectNews);
			}
		}
		shouChangResponse.setList(list);
		shouChangResponse.setResultCode("0");
		shouChangResponse.setResultDesc("success");
		return shouChangResponse;
	}
	/**
	 * 获取我的收藏
	 * @param request
	 * @return
	 */
	@RequestMapping("deleteMyShouChang")
	@ResponseBody
	public BaseResponse deleteMyShouChang(HttpServletRequest request,String id){
		BaseResponse baseResponse=new BaseResponse();
		collectNewsService.deleteMyShouChang(id);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
}
